// 请求截图权限
requestScreenCapture();
aaa();

function aaa() {
    device.keepScreenOn();

    sleep(500);
    swipe(600, 900, 100, 900, 320);
    sleep(1000);


    for (var i = 0; i < 6; i++) {
        back();
        sleep(100);
    }

    sleep(1000);

    var wx;
    for (var i = 0; i < 10; i++) {
        swipe(600, 900, 100, 900, 980);
        wx = className("android.widget.TextView").text("微信");
        if (wx.exists())
            break;
        sleep(1000);
    }

    if (!wx.exists()) {
    exit();
    }
    wx.click();

    

    // waitImg('wenjianzhushou.jpg');

    // 一直返回到桌面
    for (var i = 0; i < 10; i++) {
        back();
        wx = className("android.widget.TextView").text("微信");
        if (wx.exists())
            break;
        sleep(1000);
    }
    // 重新打开微信
    wx.click();



    // 点击微信第一个页面
    sleep(2000);
    click(98,1228);


    // 拉出小程序
    sleep(500);
    swipe(420, 180, 300, 1000, 1500);

    sleep(500);




    let target = $images.read('xiaoguonai.jpg');

    let wx = captureScreen();

    let pos = $images.findImage(wx, target);

    sleep(500);

    // 打开xiaoguonai
    toast(pos);
    click(pos.x, pos.y);

    waitImg('qudaka.jpg');
    // 点击去打卡
    click(357, 780);

    sleep(500);

    // 重进点击打卡
    daka();
}

/** 等待图片出现, 最长等待 10 秒 */
function waitImg(filePath) {
    // 等待微信加载完
    let result = $images.read(filePath);
    for (var i = 0; i < 20; i++) {
        let tmp = captureScreen();
        let pos = $images.findImage(tmp, result);
        if (null != pos) {
            toast(filePath + "已找到")
            return;
        }
        sleep(500);
    }
    sleep(500);
    toast(filePath + "未找到")
}


function daka() {
    sleep(500);
    click(357, 780);
    sleep(500);
    click(558, 92);
    sleep(2000);
    click(483, 1012);
    sleep(2000);
    click(393, 780);
    sleep(2000);
    click(346, 1080);
    sleep(2000);
    captureScreen("wxda.jpg");
    sleep(2000);
    sendmail("wxda.jpg");
}


function sendmail(filepath) {
    app.sendEmail({
        email: ["1049725878@qq.com"],
        subject: "这是一个邮件标题",
        text: "这是邮件正文",
        attachment: filepath
    });

    sleep(1000);
    click(101, 800);
    sleep(2000);

    click(654, 110);
    sleep(500);
}
